<template>
  <div class="icons-container">
    <el-tabs type="border-card" value="icons" @tab-click="handleClick">
      <el-tab-pane label="Svg Icons" name="icons">
        <el-input
          v-model="searchIconValue"
          :placeholder="$t('common.pleaseEnterKeywords')"
          clearable
        />
        <div class="grid">
          <div
            v-for="item of svgIcons"
            :key="item"
            @click="handleClipboard(generateIconCode(item), $event)"
          >
            <el-tooltip placement="top">
              <div slot="content">
                {{ generateIconCode(item) }}
              </div>
              <div class="icon-item">
                <svg-icon :icon-class="item" class-name="disabled" />
                <span>{{ item }}</span>
              </div>
            </el-tooltip>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="Element-UI Icons" name="element">
        <el-input
          v-model="searchIconValue"
          :placeholder="$t('common.pleaseEnterKeywords')"
          clearable
        />
        <div class="grid">
          <div
            v-for="item of elementIcons"
            :key="item"
            @click="handleClipboard(generateElementIconCode(item), $event)"
          >
            <el-tooltip placement="top">
              <div slot="content">
                {{ generateElementIconCode(item) }}
              </div>
              <div class="icon-item">
                <i :class="'el-icon-' + item" />
                <span>{{ item }}</span>
              </div>
            </el-tooltip>
          </div>
        </div>
      </el-tab-pane>
      <el-tab-pane label="Fontawesome Icons" name="fontawesome">
        <el-input
          v-model="searchIconValue"
          :placeholder="$t('common.pleaseEnterKeywords')"
          clearable
        />
        <div class="loading-position" />
        <div class="grid">
          <div
            v-for="item of fontawesomeIcons"
            :key="item"
            @click="handleClipboard(generateAwesomeIconCode(item), $event)"
          >
            <el-tooltip placement="top">
              <div slot="content">
                {{ generateAwesomeIconCode(item) }}
              </div>
              <div class="icon-item">
                <i :class="'awesome-icon ' + item" />
                <span>{{ item }}</span>
              </div>
            </el-tooltip>
          </div>
        </div>
      </el-tab-pane>
    </el-tabs>
  </div>
</template>

<script>
import clipboard from '@/utils/clipboard'
import svgIcons from './svg-icons'
import elementIcons from './element-icons'
import fontawesomeIcons from './fontawesome-icons'
import i18n from '@/lang'

export default {
  name: 'Icons',
  data() {
    return {
      svgIcons,
      elementIcons,
      fontawesomeIcons: [],
      searchIconValue: '',
      debounce_timer: null,
      debounce_timer_delay: 300,
      currentTabName: 'icons'
    }
  },
  // 监听输入变化
  watch: {
    searchIconValue: {
      deep: true,
      handler(newVal, oldVal) {
        if (this.debounce_timer !== null) {
          clearTimeout(this.debounce_timer)
        }
        this.debounce_timer = setTimeout(() => {
          this.filterIcon(newVal)
        }, this.debounce_timer_delay)
      }
    }
  },
  methods: {
    generateIconCode(symbol) {
      return `<svg-icon icon-class="${symbol}" />`
    },
    generateElementIconCode(symbol) {
      return `<i class="el-icon-${symbol}" />`
    },
    generateAwesomeIconCode(symbol) {
      return `<i class="awesome-icon ${symbol}" />`
    },
    handleClipboard(text, event) {
      clipboard(text, event)
    },
    handleClick(tab, event) {
      this.searchIconValue = ''
      this.currentTabName = tab.name
      if (tab.name === 'fontawesome' && this.fontawesomeIcons.length === 0) {
        const loading = this.$loading({
          lock: true,
          text: i18n.t('common.loading'),
          spinner: 'el-icon-loading',
          // background: 'rgba(0, 0, 0, 0.7)',
          target: '#pane-fontawesome .loading-position'
        })
        var page = 1
        const pageSize = 2000
        const totalPage = Math.ceil(fontawesomeIcons.length / pageSize)
        var timerId = setInterval(() => {
          const getPageIcons = fontawesomeIcons.slice(
            pageSize * (page - 1),
            pageSize * page
          )
          if (page === totalPage) {
            clearTimeout(timerId)
            loading.close()
          } else {
            page++
          }
          this.fontawesomeIcons = this.fontawesomeIcons.concat(getPageIcons)
        }, 100)
      }
    },
    // 输入检索
    filterIcon(value) {
      switch (this.currentTabName) {
        case 'element':
          this.elementIcons = elementIcons.filter((v) => v.includes(value))
          break
        case 'fontawesome':
          this.fontawesomeIcons = fontawesomeIcons.filter((v) =>
            v.includes(value)
          )
          break
        default:
          this.svgIcons = svgIcons.filter((v) => v.includes(value))
      }
    }
  }
}
</script>

<style lang="scss" scoped>
.icons-container {
  margin: 10px 20px 0;
  overflow: hidden;

  .grid {
    position: relative;
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(120px, 1fr));
  }

  .icon-item {
    margin: 20px;
    height: 85px;
    text-align: center;
    width: 100px;
    float: left;
    font-size: 30px;
    cursor: pointer;
  }

  span {
    display: block;
    font-size: 16px;
    margin-top: 10px;
  }

  .disabled {
    pointer-events: none;
  }

  .customLoading {
    height: 30px;
  }
}
</style>
